/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: RxWebMvcConfigure
 * @Package com.rx.core.bean
 * @Description: requestBody转换配置
 * @author: 陈锦韬
 * @date: 2020\7\26 0026
 * @version V1.0
 * @Copyright: 2020 陈锦韬  All rights reserved.
 */
package com.rx.core.interceptor;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.rx.core.covert.RxArgumentHandlerResolver;
import com.rx.core.covert.RxBaseDataMessageConverter;
import com.rx.core.dbase.SensitiveInfoSerialize;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.List;

/**
 * @author: Administrator
 * @Description: requestBody转换配置
 * @date: 2020\7\26 0026
 */
@Configuration
@EnableWebMvc
@ComponentScan("org.springframework.http.converter")
public class RxWebMvcConfigure implements WebMvcConfigurer {
    /**
     * 配置自定义的HttpMessageConverter 的Bean ，在Spring MVC 里注册HttpMessageConverter有两个方法：
     * 1、configureMessageConverters ：重载会覆盖掉Spring MVC 默认注册的多个HttpMessageConverter
     * 2、extendMessageConverters ：仅添加一个自定义的HttpMessageConverter ，不覆盖默认注册的HttpMessageConverter
     * 在这里重写extendMessageConverters
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

        converters.add(converters.size()-1,converter());
        converters.add(converters.size()-1,fastJsonHttpMessageConverter());
        // converters.add(1,responseBodyConverter());
    }
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        /*super.configureMessageConverters(converters);
        converters.add(responseBodyConverter());*/

    }

    @Bean
    public FastJsonConfig fastJsonConfig(){
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
//                SerializerFeature.QuoteFieldNames,
//                SerializerFeature.WriteMapNullValue,
//                SerializerFeature.DisableCircularReferenceDetect,
//                SerializerFeature.WriteDateUseDateFormat,
//                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.PrettyFormat);
        //fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 脱敏处理
        fastJsonConfig.setSerializeFilters(new SensitiveInfoSerialize());
        return fastJsonConfig;
    }

    @Bean
    public RxBaseDataMessageConverter converter() {
        RxBaseDataMessageConverter converter = new RxBaseDataMessageConverter();
        converter.setFastJsonConfig(fastJsonConfig());
        //添加自己写的拦截器
        return converter;
    }

    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        converter.setFastJsonConfig(fastJsonConfig());
        return converter;
    }


    @Bean
    public HttpMessageConverter<String> responseBodyConverter(){
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

    /**
     * 注册拦截器
     * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addInterceptors(InterceptorRegistry)
     * @Title: addInterceptors
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @param: @param registry
     * @throws
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册自定义拦截器，添加拦截路径和排除拦截路径
        registry.addInterceptor(new InterceptorConfig()).addPathPatterns("/**");
    }

    /**
     * 注册新的参数处理器
     * @param resolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new RxArgumentHandlerResolver());
    }
}
